﻿/***************************************************************************************/
// Author                 - Balaji Venkatesan
// Student ID             - 12527079
// Version                - 1.0
// CodeName               - Northodyne Solutions
// Module                 - E-Business Technologies
// Date                   - 12-September-2011
/***************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class AdminUpdateSeminar : System.Web.UI.Page
{
    public string _connStr = "Initial Catalog=DBNorthodyneSolutions; Data Source=ABC\\SQLExpress;Trusted_Connection=Yes";
    public string _userName = string.Empty;
    //SqlDataReader eventReader;
    public string _eventID = "A1";

    //global key value to update the seminar information
    public string resultantKey = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        _userName = Session["UserName"].ToString();
        //lblUserName.Text = _userName;

        Label lblusername = (Label)Master.FindControl("lblUserName");
        HyperLink hyplogin = (HyperLink)Master.FindControl("hyplogin");
        lblusername.Text = _userName;

        if (!IsPostBack)
        {

            Session["CurrentID"] = _eventID;

            using (SqlConnection connection = new SqlConnection(_connStr))
            {
                connection.Open();
                string selecteventInfo = "SELECT EventName, Venue, Date, Time, Description from Tb_Events WHERE EventID = 'A1'";


                using (SqlCommand selecteventCommand = new SqlCommand(selecteventInfo, connection))
                {
                    using (SqlDataReader eventReader = selecteventCommand.ExecuteReader())
                    {
                        while (eventReader.Read())
                        {
                            txtseminarName.Text = eventReader["EventName"].ToString();
                            txtVenue.Text = eventReader["Venue"].ToString();
                            string temp = eventReader["Date"].ToString();
                            calDate.TodaysDate = Convert.ToDateTime(temp);
                            txtTime.Text = eventReader["Time"].ToString();
                            txtDescription.Text = eventReader["Description"].ToString();
                        }

                    }
                    connection.Close();
                }
            }
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        string temp = Session["CurrentID"].ToString();
        int ActualEventID = Convert.ToInt32(temp.Substring(1));
        ActualEventID--;
        if (ActualEventID > 0)
        {
            resultantKey = "A" + ActualEventID;
            GetSeminarData(resultantKey);
            Session["CurrentID"] = resultantKey;
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        string temp = Session["CurrentID"].ToString();
        int ActualEventID = Convert.ToInt32(temp.Substring(1));
        ActualEventID++;

        SqlConnection connection = new SqlConnection(_connStr);
        connection.Open();
        string maxPropertyQuery = "SELECT max(EventID) from Tb_Events";
        SqlCommand maxCmd = new SqlCommand(maxPropertyQuery, connection);
        string maxPropertyIDValue = Convert.ToString(maxCmd.ExecuteScalar());
        int maxValue = Convert.ToInt32(maxPropertyIDValue.Substring(1));
        connection.Close();

        if (ActualEventID <= maxValue)
        {
            resultantKey = "A" + ActualEventID;
            GetSeminarData(resultantKey);
            Session["CurrentID"] = resultantKey;
        }
    }

    public void GetSeminarData(string pKey)
    {
        using (SqlConnection connection = new SqlConnection(_connStr))
        {
            connection.Open();
            string selecteventInfo = "SELECT * from Tb_Events WHERE EventID = @EventID";

            using (SqlCommand selectCommand = new SqlCommand(selecteventInfo, connection))
            {

                SqlParameter[] eveprms = new SqlParameter[1];
                eveprms[0] = new SqlParameter("@EventID", SqlDbType.NVarChar, 50);
                eveprms[0].SqlValue = pKey;

                selectCommand.Parameters.AddRange(eveprms);
                selectCommand.ExecuteNonQuery();

                using (SqlDataReader eventReader = selectCommand.ExecuteReader())
                {
                    while (eventReader.Read())
                    {
                        txtseminarName.Text = eventReader["EventName"].ToString();
                        txtVenue.Text = eventReader["Venue"].ToString();
                        string temp = eventReader["Date"].ToString();
                        calDate.TodaysDate = Convert.ToDateTime(temp);
                        txtTime.Text = eventReader["Time"].ToString();
                        txtDescription.Text = eventReader["Description"].ToString();
                    }
                }
            }
            connection.Close();
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection(_connStr))
        {
            connection.Open();
            string updateSeminarInfo = "UPDATE Tb_Events SET EventName = @EventName, Venue = @Venue, Date = @Date, Time = @Time, Description = @Description WHERE EventID = @EventID";

            using (SqlCommand updateseminarCmd = new SqlCommand(updateSeminarInfo, connection))
            {
                SqlParameter[] seminarPrms = new SqlParameter[6];
                seminarPrms[0] = new SqlParameter("@EventName", SqlDbType.NVarChar, 50);
                seminarPrms[0].SqlValue = txtseminarName.Text.Trim();

                seminarPrms[1] = new SqlParameter("@Venue", SqlDbType.NVarChar, 50);
                seminarPrms[1].SqlValue = txtVenue.Text.Trim();

                seminarPrms[2] = new SqlParameter("@Date", SqlDbType.NVarChar,50);
                //Debug this piece of code!
                if (calDate.SelectedDate.ToShortDateString() != "1/1/0001")
                {
                    seminarPrms[2].SqlValue = calDate.SelectedDate.ToShortDateString();
                }
                else
                {
                    seminarPrms[2].SqlValue = calDate.TodaysDate.ToShortDateString();
                }
                
                seminarPrms[3] = new SqlParameter("@Time", SqlDbType.NVarChar, 50);
                seminarPrms[3].SqlValue = txtTime.Text.Trim();

                seminarPrms[4] = new SqlParameter("@Description", SqlDbType.NVarChar, 50);
                seminarPrms[4].SqlValue = txtDescription.Text.Trim();

                seminarPrms[5] = new SqlParameter("@EventID", SqlDbType.NVarChar, 50);
                seminarPrms[5].SqlValue = Session["CurrentID"].ToString();

                updateseminarCmd.Parameters.AddRange(seminarPrms);
                updateseminarCmd.ExecuteNonQuery();
            }

            connection.Close();
        }
        Response.Redirect("AdminViewSeminar.aspx");
    }
}